package com.shanghaionstar.mro;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.onstar.cn.common.util.CommonUtil;
import com.onstar.cn.framework.annotation.FindViewById;
import com.onstar.cn.weight.CustomImageView;
import com.shanghaionstar.R;
import com.shanghaionstar.enumtype.GaodeType;
import com.shanghaionstar.mro.bean.weather.Item;
import com.shanghaionstar.mro.bean.weather.WeatherBean;
import com.shanghaionstar.mro.util.MrOWeatherUtil;
import com.shanghaionstar.mro.util.WeatherType;

import java.util.Iterator;
import java.util.List;

public class MroWeatherFragment extends MrOBaseFragment {
    private View view;
    private Activity activity;
    @FindViewById
    private TextView tv_weather_description, tv_weather_air_quality, tv_weather_city, tv_weather_desc;
    @FindViewById(background = R.drawable.mro_weather_two)
    private CustomImageView iv_weather_lowest_temperature_ten;
    @FindViewById(background = R.drawable.mro_weather_one)
    private CustomImageView iv_weather_lowest_temperature_one;
    @FindViewById(background = R.drawable.mro_weather_three)
    private CustomImageView iv_weather_highest_temperature_ten;
    @FindViewById(background = R.drawable.mro_weather_one)
    private CustomImageView iv_weather_highest_temperature_one;
    @FindViewById(background = R.drawable.mro_weather_cloudy)
    private CustomImageView iv_weather_desc;
    @FindViewById(background = R.drawable.mro_weather_negative)
    private CustomImageView iv_weather_highest_negative, iv_weather_lowest_negative;
    @FindViewById(background = R.drawable.mro_weather_line)
    private LinearLayout mro_weather_line;
    @FindViewById
    private LinearLayout ll_weather;

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (view == null) {
            view = inflater.inflate(R.layout.mro_fragment_weather, container, false);
            injectView(view);
        } else {
            super.onResumeView(view);
        }
        return view;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        this.activity = activity;
    }

    @Override
    public void onResume() {
        super.onResume();
        Bundle bundle = getArguments();
        WeatherBean bean = null;
        if (!CommonUtil.isNull(bundle)) {
            bean = (WeatherBean) bundle.getSerializable(GaodeType.Weather.value());
        } else {
            tv_weather_description.setText(getString(R.string.SB005_MSG003));
            ll_weather.setVisibility(View.GONE);
            return;
        }
        int minTemperature = 0, maxTemperature = 0;
        String weatherDesc = null;
        if (bean!=null && !CommonUtil.isNull(bean.getAts().getWeather().getForecast().getDay())) {
            List<Item> items = bean.getAts().getWeather().getForecast().getDay().get(0).getItem();
            Item forecast = null;
            if (!CommonUtil.isNull(items)) {
                Iterator it = items.iterator();
                while (it.hasNext()) {
                    forecast = (Item) it.next();
                    if (forecast.getDaynight().equals("1")) {
                        maxTemperature = Integer.valueOf(forecast.getTemperature());
                        weatherDesc = forecast.getWeather_desc();
                    } else if (forecast.getDaynight().equals("2")) {
                        minTemperature = Integer.valueOf(forecast.getTemperature());
                        weatherDesc = forecast.getWeather_desc();
                    }

                }
            }
            setImage(weatherDesc, iv_weather_desc);
            if (!CommonUtil.isNull(bean.getAts())) {
                tv_weather_city.setText(bean.getAts().getCity());
            }
            tv_weather_desc.setText(weatherDesc);
            //个位，十位
            int unit, decade;
            if (!CommonUtil.isNull(minTemperature)) {
                int temp = 0;
                if (minTemperature < 0) {
                    temp = Math.abs(minTemperature);
                    iv_weather_lowest_negative.setVisibility(View.VISIBLE);
                } else {
                    temp = minTemperature;
                    iv_weather_lowest_negative.setVisibility(View.GONE);
                }
                if (0 <= temp && temp < 10) {
                    unit = temp;
                    setTemperatureImage(iv_weather_lowest_temperature_one, unit);
                    iv_weather_lowest_temperature_ten.setVisibility(View.GONE);
                } else if (10 <= temp && temp < 100) {
                    unit = temp % 10;
                    decade = temp / 10;
                    setTemperatureImage(iv_weather_lowest_temperature_one, unit);
                    setTemperatureImage(iv_weather_lowest_temperature_ten, decade);
                }
            }
            if (!CommonUtil.isNull(maxTemperature)) {
                int temp = 0;
                if (maxTemperature < 0) {
                    temp = Math.abs(maxTemperature);
                    iv_weather_highest_negative.setVisibility(View.VISIBLE);
                } else {
                    temp = maxTemperature;
                    iv_weather_highest_negative.setVisibility(View.GONE);
                }
                if (0 <= temp && temp < 10) {
                    unit = temp;
                    setTemperatureImage(iv_weather_highest_temperature_one, unit);
                    iv_weather_highest_temperature_ten.setVisibility(View.GONE);
                } else if (10 <= temp && temp < 100) {
                    unit = temp % 10;
                    decade = temp / 10;
                    setTemperatureImage(iv_weather_highest_temperature_one, unit);
                    setTemperatureImage(iv_weather_highest_temperature_ten, decade);
                }
            }
            String airQuality = null;
            if (!CommonUtil.isNull(bean.getAts().getWeather().getLivingindex().getDay())) {
                List<Item> list = bean.getAts().getWeather().getLivingindex().getDay().get(0).getItem();
                Item livingindex = null;
                if (!CommonUtil.isNull(list)) {
                    Iterator it = list.iterator();
                    while (it.hasNext()) {
                        livingindex = (Item) it.next();
                        if (livingindex.getName().equals(getString(R.string.mro_weather_pollution_index))) {
                            airQuality = livingindex.getValue();
                        }
                    }
                }
                tv_weather_air_quality.setText(airQuality);
            }
            MrOWeatherUtil weatherUtil = new MrOWeatherUtil(getActivity());
            if (!CommonUtil.isNull(weatherUtil.getWeatherDec(weatherDesc))) {
                tv_weather_description.setText(bean.getAts().getCity() + getString(R.string.mro_comma) + weatherDesc + getString(R.string.mro_comma) + getString(R.string.mro_weather_highest_temperature) + maxTemperature
                        + getString(R.string.mro_weather_degree) + getString(R.string.mro_comma) + getString(R.string.mro_weather_lowest_temperature) + minTemperature + getString(R.string.mro_weather_degree) + getString(R.string.mro_comma) + weatherUtil.getWeatherDec(weatherDesc));
            } else {
                tv_weather_description.setText(bean.getAts().getCity() + getString(R.string.mro_comma) + weatherDesc + getString(R.string.mro_comma) + getString(R.string.mro_weather_highest_temperature) + maxTemperature
                        + getString(R.string.mro_weather_degree) + getString(R.string.mro_comma) + getString(R.string.mro_weather_lowest_temperature) + minTemperature + getString(R.string.mro_weather_degree));
            }
        }
    }


    /**
     * @param @param temperature    设定文件
     * @return void    返回类型
     * @throws
     * @Title: setTemperature
     * @Description: 根据温度显示图片
     */
    private void setTemperatureImage(CustomImageView customImageView, int temperature) {
        switch (temperature) {
            case 0:
                customImageView.setImageResource(R.drawable.mro_weather_zero);
                break;
            case 1:
                customImageView.setImageResource(R.drawable.mro_weather_one);
                break;
            case 2:
                customImageView.setImageResource(R.drawable.mro_weather_two);
                break;
            case 3:
                customImageView.setImageResource(R.drawable.mro_weather_three);
                break;
            case 4:
                customImageView.setImageResource(R.drawable.mro_weather_four);
                break;
            case 5:
                customImageView.setImageResource(R.drawable.mro_weather_five);
                break;
            case 6:
                customImageView.setImageResource(R.drawable.mro_weather_six);
                break;
            case 7:
                customImageView.setImageResource(R.drawable.mro_weather_seven);
                break;
            case 8:
                customImageView.setImageResource(R.drawable.mro_weather_eight);
                break;
            case 9:
                customImageView.setImageResource(R.drawable.mro_weather_nine);
                break;

            default:
                break;
        }
    }

    /**
     * @param @param desc
     * @param @param imageView    设定文件
     * @return void    返回类型
     * @throws
     * @Title: setImage
     * @Description: 根据天气描述设置图片
     */
    private void setImage(String desc, ImageView imageView) {
        WeatherType type = WeatherType.getWeatherTypeByDesc(desc, activity);
        if (type == null) {
            return;
        }
        switch (type) {
            case SNOWSTORM:
                imageView.setImageResource(R.drawable.mro_weather_snowstorm);
                break;
            case RAINSTORM:
                imageView.setImageResource(R.drawable.mro_weather_rainstorm);
                break;
            case RAINSTORM_TURN_HEAVYRAINSTORM:
                imageView.setImageResource(R.drawable.mro_weather_rainstorm_turn_heavyrainstorm);
                break;
            case SQUALL:
                imageView.setImageResource(R.drawable.mro_weather_squall);
                break;
            case HEAVYRAINSTORM:
                imageView.setImageResource(R.drawable.mro_weather_heavyrainstorm);
                break;
            case HEAVYRAINSTORM_TURN_MOSTRAINSTORM:
                imageView.setImageResource(R.drawable.mro_weather_heavyrainstorm_turn_mostrainstorm);
                break;
            case HEAVYSNOW:
                imageView.setImageResource(R.drawable.mro_weather_heavysnow);
                break;
            case HEAVYSNOW_TURN_SNOWSTORM:
                imageView.setImageResource(R.drawable.mro_weather_heavysnow_turn_snowstorm);
                break;
            case HEAVYRAIN:
                imageView.setImageResource(R.drawable.mro_weather_heavyrain);
                break;
            case HEAVYRAIN_TURN_RAINSTORM:
                imageView.setImageResource(R.drawable.mro_weather_heavyrain_turn_rainstorm);
                break;
            case FREEZINGRAIN:
                imageView.setImageResource(R.drawable.mro_weather_freezingrain);
                break;
            case CLOUDY:
                imageView.setImageResource(R.drawable.mro_weather_cloudy);
                break;
            case DUST:
                imageView.setImageResource(R.drawable.mro_weather_dust);
                break;
            case THUNDERSHOWER:
                imageView.setImageResource(R.drawable.mro_weather_thundershower);
                break;
            case THUNDERSHOWER_HAIL:
                imageView.setImageResource(R.drawable.mro_weather_thundershower_hail);
                break;
            case TORNADO:
                imageView.setImageResource(R.drawable.mro_weather_tornado);
                break;
            case HAZE:
                imageView.setImageResource(R.drawable.mro_weather_haze);
                break;
            case STRONG_SANDSTORM:
                imageView.setImageResource(R.drawable.mro_weather_strong_sandstorm);
                break;
            case MIST:
                imageView.setImageResource(R.drawable.mro_weather_mist);
                break;
            case SUNNY:
                imageView.setImageResource(R.drawable.mro_weather_sunny);
                break;
            case BLOWINGSNOW:
                imageView.setImageResource(R.drawable.mro_weather_blowingsnow);
                break;
            case SANDSTORM:
                imageView.setImageResource(R.drawable.mro_weather_sandstorm);
                break;
            case MOSTRAINSTORM:
                imageView.setImageResource(R.drawable.mro_weather_mostrainstorm);
                break;
            case FOG:
                imageView.setImageResource(R.drawable.mro_weather_fog);
                break;
            case SLIGHTSNOW:
                imageView.setImageResource(R.drawable.mro_weather_slightsnow);
                break;
            case SLIGHTSNOW_TURN_SNOW:
                imageView.setImageResource(R.drawable.mro_weather_slightsnow_turn_snow);
                break;
            case LIGHTRAIN:
                imageView.setImageResource(R.drawable.mro_weather_lightrain);
                break;
            case LIGHTRAIN_TURN_RAIN:
                imageView.setImageResource(R.drawable.mro_weather_lightrain_turn_rain);
                break;
            case BLOWINGSAND:
                imageView.setImageResource(R.drawable.mro_weather_blowingsand);
                break;
            case OVERCAST:
                imageView.setImageResource(R.drawable.mro_weather_overcast);
                break;
            case RAIN_SNOW:
                imageView.setImageResource(R.drawable.mro_weather_rain_snow);
                break;
            case SNOWSHOWER:
                imageView.setImageResource(R.drawable.mro_weather_snowshower);
                break;
            case SHOWER:
                imageView.setImageResource(R.drawable.mro_weather_shower);
                break;
            case SNOW:
                imageView.setImageResource(R.drawable.mro_weather_snow);
                break;
            case SNOW_TURN_HEAVYSNOW:
                imageView.setImageResource(R.drawable.mro_weather_snow_turn_heavysnow);
                break;
            case RAIN:
                imageView.setImageResource(R.drawable.mro_weather_rain);
                break;
            case RAIN_TURN_HEAVYRAIN:
                imageView.setImageResource(R.drawable.mro_weather_rain_turn_heavyrain);
                break;
        }
    }

    // 5.0 crash
    @Override
    public void onDestroy() {
        super.onDestroy();
        view = null;
    }
}
